/**
 * @file    WebOleInPlaceSite.cpp
 * @brief   
 * @date    2005-03-15 15:55:23
 * @author  GengYong (gengyong@gmail.com)
 *
 */

#include <cassert>
#include "WebOleInPlaceSite.h"
#include "WebOleInPlaceUIWindow.h"
#include "WebOleInPlaceFrame.h"

using namespace xessage_private;

WebOleInPlaceSite::WebOleInPlaceSite(WebWindowImpl * app):m_appImpl(app), m_accel(0), m_entries(0), m_refCount(1)
{
	m_inPlaceUIWindow = new WebOleInPlaceUIWindow(app);
	m_inPlaceFrame = new WebOleInPlaceFrame(app);
	OnBuildAccelerator(&(this->m_accel), &(this->m_entries));
	return;
}

WebOleInPlaceSite::~WebOleInPlaceSite()
{
	if (this->m_accel != NULL)
	{
		DestroyAcceleratorTable(m_accel);
		m_accel = NULL;
	}

	if (m_inPlaceFrame)
	{
		m_inPlaceFrame->Release();
		m_inPlaceFrame = NULL;
	}

	if (m_inPlaceUIWindow)
	{
		m_inPlaceUIWindow->Release();
		m_inPlaceUIWindow = NULL;
	}
	return;
}

HRESULT WebOleInPlaceSite::QueryInterface(REFIID riid, LPVOID * ppvObject)
{
	if (ppvObject == NULL) return E_POINTER;

	if ((riid == IID_IUnknown) || (riid == IID_IOleInPlaceSite))
	{
		this->AddRef();
		*ppvObject = this;
	}
	else if (riid == IID_IOleInPlaceUIWindow)
	{
		return this->m_inPlaceUIWindow->QueryInterface(riid, ppvObject);
	}
	else if (riid == IID_IOleInPlaceFrame)
	{
		return this->m_inPlaceFrame->QueryInterface(riid, ppvObject);
	}	
	else
	{
		*ppvObject = NULL;
		return E_NOINTERFACE;
	}
	return S_OK;
}


ULONG WebOleInPlaceSite::AddRef()
{
	return InterlockedIncrement(&m_refCount);
}

ULONG WebOleInPlaceSite::Release()
{
	LONG result = InterlockedDecrement(&m_refCount);
	assert(result >= 0);
	if (result == 0)
	{
		delete this;
	}
	return (ULONG)(result);
}


HRESULT WebOleInPlaceSite::GetWindow(HWND * lphwnd)
{	
	return m_inPlaceFrame->GetWindow(lphwnd);	
}

HRESULT WebOleInPlaceSite::ContextSensitiveHelp(BOOL fEnterMode)
{
	return E_NOTIMPL;;
}

HRESULT WebOleInPlaceSite::CanInPlaceActivate()
{
	return S_OK;
}

HRESULT WebOleInPlaceSite::OnInPlaceActivate()
{
	return S_OK;
}

HRESULT WebOleInPlaceSite::OnUIActivate()
{
	return S_OK;
}

HRESULT WebOleInPlaceSite::GetWindowContext(LPOLEINPLACEFRAME * lplpFrame, LPOLEINPLACEUIWINDOW * lplpDoc, LPRECT lprcPosRect, LPRECT lprcClipRect, LPOLEINPLACEFRAMEINFO lpFrameInfo)
{
	if (lplpFrame != NULL)
	{
		m_inPlaceFrame->QueryInterface(IID_IUnknown, (LPVOID *)lplpFrame);
	}
	if (lplpDoc != NULL) *lplpDoc = NULL;
	if (lpFrameInfo != NULL)
	{
		lpFrameInfo->fMDIApp = FALSE;
		m_inPlaceFrame->GetWindow(&(lpFrameInfo->hwndFrame));
		lpFrameInfo->haccel = m_accel;
		lpFrameInfo->cAccelEntries = m_entries;
		if (lprcPosRect != NULL) 
		{
			GetClientRect(lpFrameInfo->hwndFrame, lprcPosRect);
		}
		if (lprcClipRect != NULL)
		{
			GetClientRect(lpFrameInfo->hwndFrame, lprcClipRect);
		}
	}
	return(S_OK);
}

HRESULT WebOleInPlaceSite::Scroll(SIZE scrollExtent)
{
	return E_NOTIMPL;;
}

HRESULT WebOleInPlaceSite::OnUIDeactivate(BOOL fUndoable)
{
	return S_OK;
}

HRESULT WebOleInPlaceSite::OnInPlaceDeactivate()
{
	return S_OK;
}

HRESULT WebOleInPlaceSite::DiscardUndoState()
{
	return E_NOTIMPL;;
}

HRESULT WebOleInPlaceSite::DeactivateAndUndo()
{
	return E_NOTIMPL;;
}

HRESULT WebOleInPlaceSite::OnPosRectChange(LPCRECT lprcPosRect)
{
	return S_OK;
}


HRESULT WebOleInPlaceSite::OnBuildAccelerator(HACCEL * lpaccel, UINT * lpEntries)
{
	return S_OK;
}


